<?php

namespace addons\csmitsm\library\xcore\xcore\utils;

use ReflectionClass;

/**
 * 服务启动:
 * php think queue:work --daemon
 * 
 * 配置修改:application/extra/queue.php
 * Database模式
    return [
        'connector' => 'database',
        'expire'  => 60,
        'default' => 'default',
        'table'   => 'jobs',
        'dsn'     => [
            'type' => 'mysql',
            'database' => 'local_csmitsm',
            'hostname' => '127.0.0.1',
            'username' => 'root',
            'password' => 'root',
            'prefix' => 'fa_',
        ]    
    ];
 * Redis模式
    return [
        'connector'  => 'Redis',          // Redis 驱动
        'expire'     => 0,             // 任务的过期时间，默认为60秒; 若要禁用，则设置为 null
        'default'    => 'default',    // 默认的队列名称
        'host'       => '127.0.0.1',       // redis 主机ip
        'port'       => 6379,        // redis 端口
        'password'   => '',             // redis 密码
        'select'     => 0,          // 使用哪一个 db，默认为 db0
        'timeout'    => 0,          // redis连接的超时时间
        'persistent' => false,
    ];
 * 
 * 数据库模式
    CREATE TABLE `fa_jobs` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `queue` VARCHAR(255) NOT NULL,
    `payload` LONGTEXT NOT NULL,
    `attempts` TINYINT(3) UNSIGNED NOT NULL,
    `reserved` TINYINT(3) UNSIGNED NOT NULL,
    `reserved_at` INT(10) UNSIGNED DEFAULT NULL,
    `available_at` INT(10) UNSIGNED NOT NULL,
    `created_at` INT(10) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8;
 * 
 * Docs: 
 * https://github.com/top-think/think-queue/tree/v2.0.4
 * 
 * Windows redis下载地址:
 * https://gitcode.com/open-source-toolkit/4340f/blob/main/redis-windows-7.2.3.zip
 */
class XcThreadJobUtils
{
    public static function execute($jobClsname, $method, ...$param)
    {
        $xcthreadjob = XcConfigUtils::config("xcthreadjob");
        if($xcthreadjob=='Y'){
            \think\Queue::push($jobClsname, [
                'method' => $method,
                'param' => $param
            ]);            
        }else{
            $reflector = new ReflectionClass($jobClsname);
            $instance = $reflector->newInstance();
            $methodReflector = $reflector->getMethod($method);
            $methodReflector->invokeArgs($instance, $param);
        }
    }
}
